<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>客户区可见范围限制拖拽</title>
    <style type="text/css">
        * {
            padding: 0;
            margin: 0;
        }
        
        #div1 {
            width: 600px;
            height: 500px;
            padding: 2px;
            background: orange;
            position: relative;
            left: 100px;
            top: 30px;
        }
        
        #div2 {
            width: 200px;
            height: 100px;
            padding: 2px;
            background: black;
            position: absolute;
            border: 1px solid blue;
        }
    </style>
</head>
<body>
<div id="div1">
    <div id="div2"></div>
</div>

<script type="text/javascript">
    var oDiv1 = document.getElementById('div1');
    var oDiv2 = document.getElementById('div2');

    // function getStyle(obj, attr) {
    //  if (obj.currentStyle) {
    //   return obj.currentStyle[attr];
    //  } else {
    //   return getComputedStyle(obj, null)[attr];
    //  }
    // }
    oDiv2.onmousedown = function (ev) {
        var oEvent = ev || event;
        // clientX 事件属性返回当事件被触发时鼠标指针向对于浏览器页面（或客户区）的水平坐标。
        // offsetWidth=(padding)*2+div1Width
        // offsetWidth=border*2+(padding)*2+div2Width
        //    offsetHeight=(padding)*2+div1Height
        // offsetHeight=border*2+(padding)*2+div2Height

        var disX = oEvent.clientX - oDiv2.offsetLeft;

        var disY = oEvent.clientY - oDiv2.offsetTop;
        console.log(disX)
        console.log(disY)
        // var disX = oEvent.clientX - parseInt(getStyle(oDiv2, 'left'));
        // var disY = oEvent.clientY - parseInt(getStyle(oDiv2, 'top'));
        document.onmousemove = function (ev) {
            var oEvent = ev || event;
            var l = oEvent.clientX - disX;
            var t = oEvent.clientY - disY;
            console.log(oDiv1.offsetWidth)
            console.log(oDiv2.offsetWidth)
            console.log(oDiv1.offsetHeight)
            console.log(oDiv2.offsetHeight)
// offsetWidth 是对象的可见宽度，包滚动条等边线，会随窗口的显示大小改变
            if (l < 0) {
                l = 0;
            } else if (l > oDiv1.offsetWidth - /*parseInt(getStyle(oDiv2,'width'))*/oDiv2.offsetWidth) {
                l = oDiv1.offsetWidth - oDiv2.offsetWidth;
            }
            if (t < 0) {
                t = 0;
            } else if (t > oDiv1.offsetHeight - oDiv2.offsetHeight) {
                t = oDiv1.offsetHeight - oDiv2.offsetHeight;
            }
            oDiv2.style.left = l + 'px';
            oDiv2.style.top = t + 'px';
        };


        document.onmouseup = function () {
            document.onmousemove = null;//如果不取消，鼠标弹起div依旧会随着鼠标移动
            document.onmouseup = null;
        };
    };
</script>
</body>
</html>


<!-- <!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>自定义滚动条</title>
 <style type="text/css">
 #div1 {
  width: 600px;
  height: 20px;
  background: orange;
  position: relative;
  margin: 50px auto;
 }
 #div2 {
  width: 20px;
  height: 20px;
  background: green;
  position: absolute;
 }
 #div3 {
  width: 0;
  height: 0;
  margin: 20px auto;
 }
 #div3 img {
  width: 100%;
  height: 100%;
 }
 </style>
</head>
<body>
 <div id="div1">
 <div id="div2"></div>
 </div>
 <div id="div3">
 ![](https://timgsa.baidu.com/141128%2F201411281041075742.jpg)
 </div>
 <script type="text/javascript">
 var oDiv1 = document.getElementById('div1');
 var oDiv2 = document.getElementById('div2');
 var oDiv3 = document.getElementById('div3');
 oDiv2.onmousedown = function(ev) {
  var oEvent = ev || event;
  var disX = oEvent.clientX - oDiv2.offsetLeft;

  document.onmousemove = function(ev) {
  var oEvent = ev || event;
  var l = oEvent.clientX - disX;
  if (l < 0) {
   l = 0;
  } else if (l > oDiv1.offsetWidth - oDiv2.offsetWidth) {
   l = oDiv1.offsetWidth - oDiv2.offsetWidth;
  }
  oDiv2.style.left = l + 'px';//l范围：[0,580]
  //document.title = l / 580; //范围：[0,1]
  var ratio = oDiv1.offsetWidth - oDiv2.offsetWidth;
  var scale = l / ratio;
  var w = 600 * scale;
  var h = 370 * scale;
  console.log(w);
  oDiv3.style.cssText = ';width:' + w + 'px;height:' + h +'px;';

  };

  document.onmouseup = function() {
  document.onmousemove = null;
  document.onmouseup = null;
  };
 };
 </script>
</body>
</html> -->
